<?php
class Banking_balance extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('fund_model','fund');
		$this->load->model('voucher_model','voucher');
		$this->load->model('voucher_balance_model','balance');
		$this->load->model('voucher_check_model','check');
		$this->load->model('voucher_receive_paid_model','receive_paid');
		$this->load->model('voucher_detail_model','voucher_detail');
	}
	//**** งบพิสูจน์ยอดเงินฝาก ******
	function index($fund_id)
	{
		if($_POST)
		{
			$this->db->debug = TRUE;
			$_POST['user_id'] = $this->session->userdata('id');
			$_POST['agency_id'] = login_data('agency_id');
			$_POST['balance'] = str_replace(",", "", $_POST['balance']);
			$id = $this->balance->save($_POST);
			$this->check->delete('voucher_balance_id',$id);
			$this->receive_paid->delete('voucher_balance_id',$id);
			foreach($_POST['check']['check_date'] as $key => $item)
			{
				$check['check_date'] = $item;
				$check['number'] = $_POST['check']['number'][$key];
				$check['receive'] = $_POST['check']['receive'][$key];
				$check['name'] = $_POST['check']['name'][$key];
				$check['price'] = str_replace(",", "", $_POST['check']['price'][$key]);
				$check['voucher_balance_id'] = $id;
				
				if(($check['price']<>"")&&($check['price']<>'0.00'))
				{
					$this->check->save($check);
				}
				unset($check);
			}
			
			foreach($_POST['receive']['name'] as $key => $item)
			{
				$receive['name'] = $item;
				$receive['price'] = str_replace(",", "", $_POST['receive']['price'][$key]);
				$receive['voucher_balance_id'] = $id;
				
				if(($receive['price']<>"")&&($receive['price']<>'0.00'))
				{
					$this->receive_paid->save($receive);
				}
				unset($receive);
			}
			
			Modules::run('files/save_file', $_POST['fund_id'], ($_POST['year_record']), $_POST['month_record'], $_POST['agency_id'], 'banking_balance');	
			
			set_notify('success', lang('save_data_complete'));
			redirect('voucher/banking_balance/'.$fund_id.'?month='.$_POST['month_record'].'&year='.($_POST['year_budget'] + 543));
		}
		//$this->db->debug = TRUE;
		$user = login_data();
		$data['fund'] = $this->fund->get_row($fund_id);
		list($year,$month,$data['year'],$data['month']) = last_voucher_date($fund_id);
		$data['year1'] = $year;
		$data['month1'] = $month;
		$agency_id = (level_data('view'))?(isset($_GET['agency_id']))?$_GET['agency_id']:$user['agency_id']:$user['agency_id'];
		$data['agency_id'] = $agency_id;
		$balance_where = ' and YEAR(vouchers.voucher_date) = '.$year.' and MONTH(vouchers.voucher_date) = '.$month.' and vouchers.agency_id = '.$agency_id;
		$data['deposits'] = $this->db->getone('
		select 
		(COALESCE(c.dr_balance,0) - COALESCE(d.cr_balance,0) + COALESCE(dr_month,0) - COALESCE(cr_month,0)) price
		from accounts
		left join (select account_id,sum(price) dr_month from voucher_details join vouchers on voucher_details.voucher_id=vouchers.id where "TYPE" = \'dr\' and voucher_id in (select id from vouchers where is_balance is null and mode is null ) '.$balance_where.' group by account_id) a
		on a.account_id = accounts.id
		left join (select account_id,sum(price) cr_month from voucher_details join vouchers on voucher_details.voucher_id=vouchers.id where "TYPE" = \'cr\' and voucher_id in (select id from vouchers where is_balance is null and mode is null ) '.$balance_where.' group by account_id) b
		on b.account_id = accounts.id
		left join (select account_id,sum(price) dr_balance from voucher_details join vouchers on voucher_details.voucher_id=vouchers.id where "TYPE" = \'dr\' and voucher_id in (select id from vouchers where is_balance = 1 and mode is null ) '.$balance_where.' group by account_id) c
		on c.account_id = accounts.id
		left join (select account_id,sum(price) cr_balance from voucher_details join vouchers on voucher_details.voucher_id=vouchers.id where "TYPE" = \'cr\' and voucher_id in (select id from vouchers where is_balance = 1 and mode is null ) '.$balance_where.' group by account_id) d
		on d.account_id = accounts.id
		where fund_id = '.$fund_id.'
		and accounts.id = 3
		',FALSE);
		
		
		$data['balance'] = $this->db->getrow('select * from voucher_balances where month_record = '.$month.' and year_record = '.$year.' and agency_id = '.$agency_id);
		dbConvert($data['balance']);
		
		if($data['balance']&&(@$_GET['mode']<>'edit'))
		{
			$data['checks'] = $this->fund->get('select * from voucher_checks where voucher_balance_id = '.$data['balance']['id'],FALSE);
			$data['receives'] = $this->fund->get('select * from voucher_receive_paids where voucher_balance_id = '.$data['balance']['id'],FALSE);
			$this->template->append_metadata(js_print());
			$this->template->build('voucher_banking_balance_view',$data);
		}
		else
		{
			if($data['balance'])
			{
			$data['checks'] = $this->fund->get('select * from voucher_checks where voucher_balance_id = '.$data['balance']['id'],FALSE);
			$data['receives'] = $this->fund->get('select * from voucher_receive_paids where voucher_balance_id = '.$data['balance']['id'],FALSE);
			}
			$this->template->append_metadata(js_validate());
			$this->template->append_metadata(js_datepicker());
			$this->template->build('voucher_banking_balance',$data);
		}
	}
	
	
	
}
?>